<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>pubsub_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_pubsub_pubsub_test.js.html">pubsub_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.pubsub.PubSubTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.pubsub.PubSubTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.array&#39;);
<a name="line19"></a>goog.require(&#39;goog.pubsub.PubSub&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>
<a name="line22"></a>var pubsub;
<a name="line23"></a>
<a name="line24"></a>function setUp() {
<a name="line25"></a>  pubsub = new goog.pubsub.PubSub();
<a name="line26"></a>}
<a name="line27"></a>
<a name="line28"></a>function tearDown() {
<a name="line29"></a>  pubsub.dispose();
<a name="line30"></a>}
<a name="line31"></a>
<a name="line32"></a>function testConstructor() {
<a name="line33"></a>  assertNotNull(&#39;PubSub instance must not be null&#39;, pubsub);
<a name="line34"></a>  assertTrue(&#39;PubSub instance must have the expected type&#39;,
<a name="line35"></a>      pubsub instanceof goog.pubsub.PubSub);
<a name="line36"></a>}
<a name="line37"></a>
<a name="line38"></a>function testDispose() {
<a name="line39"></a>  assertFalse(&#39;PubSub instance must not have been disposed of&#39;,
<a name="line40"></a>      pubsub.isDisposed());
<a name="line41"></a>  pubsub.dispose();
<a name="line42"></a>  assertTrue(&#39;PubSub instance must have been disposed of&#39;,
<a name="line43"></a>      pubsub.isDisposed());
<a name="line44"></a>}
<a name="line45"></a>
<a name="line46"></a>function testSubscribeUnsubscribe() {
<a name="line47"></a>  function foo1() {
<a name="line48"></a>  }
<a name="line49"></a>  function bar1() {
<a name="line50"></a>  }
<a name="line51"></a>  function foo2() {
<a name="line52"></a>  }
<a name="line53"></a>  function bar2() {
<a name="line54"></a>  }
<a name="line55"></a>
<a name="line56"></a>  assertEquals(&#39;Topic &quot;foo&quot; must not have any subscribers&#39;, 0,
<a name="line57"></a>      pubsub.getCount(&#39;foo&#39;));
<a name="line58"></a>  assertEquals(&#39;Topic &quot;bar&quot; must not have any subscribers&#39;, 0,
<a name="line59"></a>      pubsub.getCount(&#39;bar&#39;));
<a name="line60"></a>
<a name="line61"></a>  pubsub.subscribe(&#39;foo&#39;, foo1);
<a name="line62"></a>  assertEquals(&#39;Topic &quot;foo&quot; must have 1 subscriber&#39;, 1,
<a name="line63"></a>      pubsub.getCount(&#39;foo&#39;));
<a name="line64"></a>  assertEquals(&#39;Topic &quot;bar&quot; must not have any subscribers&#39;, 0,
<a name="line65"></a>      pubsub.getCount(&#39;bar&#39;));
<a name="line66"></a>
<a name="line67"></a>  pubsub.subscribe(&#39;bar&#39;, bar1);
<a name="line68"></a>  assertEquals(&#39;Topic &quot;foo&quot; must have 1 subscriber&#39;, 1,
<a name="line69"></a>      pubsub.getCount(&#39;foo&#39;));
<a name="line70"></a>  assertEquals(&#39;Topic &quot;bar&quot; must have 1 subscriber&#39;, 1,
<a name="line71"></a>      pubsub.getCount(&#39;bar&#39;));
<a name="line72"></a>
<a name="line73"></a>  pubsub.subscribe(&#39;foo&#39;, foo2);
<a name="line74"></a>  assertEquals(&#39;Topic &quot;foo&quot; must have 2 subscribers&#39;, 2,
<a name="line75"></a>      pubsub.getCount(&#39;foo&#39;));
<a name="line76"></a>  assertEquals(&#39;Topic &quot;bar&quot; must have 1 subscriber&#39;, 1,
<a name="line77"></a>      pubsub.getCount(&#39;bar&#39;));
<a name="line78"></a>
<a name="line79"></a>  pubsub.subscribe(&#39;bar&#39;, bar2);
<a name="line80"></a>  assertEquals(&#39;Topic &quot;foo&quot; must have 2 subscribers&#39;, 2,
<a name="line81"></a>      pubsub.getCount(&#39;foo&#39;));
<a name="line82"></a>  assertEquals(&#39;Topic &quot;bar&quot; must have 2 subscribers&#39;, 2,
<a name="line83"></a>      pubsub.getCount(&#39;bar&#39;));
<a name="line84"></a>
<a name="line85"></a>  assertTrue(pubsub.unsubscribe(&#39;foo&#39;, foo1));
<a name="line86"></a>  assertEquals(&#39;Topic &quot;foo&quot; must have 1 subscriber&#39;, 1,
<a name="line87"></a>      pubsub.getCount(&#39;foo&#39;));
<a name="line88"></a>  assertEquals(&#39;Topic &quot;bar&quot; must have 2 subscribers&#39;, 2,
<a name="line89"></a>      pubsub.getCount(&#39;bar&#39;));
<a name="line90"></a>
<a name="line91"></a>  assertTrue(pubsub.unsubscribe(&#39;foo&#39;, foo2));
<a name="line92"></a>  assertEquals(&#39;Topic &quot;foo&quot; must have no subscribers&#39;, 0,
<a name="line93"></a>      pubsub.getCount(&#39;foo&#39;));
<a name="line94"></a>  assertEquals(&#39;Topic &quot;bar&quot; must have 2 subscribers&#39;, 2,
<a name="line95"></a>      pubsub.getCount(&#39;bar&#39;));
<a name="line96"></a>
<a name="line97"></a>  assertTrue(pubsub.unsubscribe(&#39;bar&#39;, bar1));
<a name="line98"></a>  assertEquals(&#39;Topic &quot;foo&quot; must have no subscribers&#39;, 0,
<a name="line99"></a>      pubsub.getCount(&#39;foo&#39;));
<a name="line100"></a>  assertEquals(&#39;Topic &quot;bar&quot; must have 1 subscriber&#39;, 1,
<a name="line101"></a>      pubsub.getCount(&#39;bar&#39;));
<a name="line102"></a>
<a name="line103"></a>  assertTrue(pubsub.unsubscribe(&#39;bar&#39;, bar2));
<a name="line104"></a>  assertEquals(&#39;Topic &quot;foo&quot; must have no subscribers&#39;, 0,
<a name="line105"></a>      pubsub.getCount(&#39;foo&#39;));
<a name="line106"></a>  assertEquals(&#39;Topic &quot;bar&quot; must have no subscribers&#39;, 0,
<a name="line107"></a>      pubsub.getCount(&#39;bar&#39;));
<a name="line108"></a>
<a name="line109"></a>  assertFalse(&#39;Unsubscribing a nonexistent topic must return false&#39;,
<a name="line110"></a>      pubsub.unsubscribe(&#39;baz&#39;, foo1));
<a name="line111"></a>
<a name="line112"></a>  assertFalse(&#39;Unsubscribing a nonexistent function must return false&#39;,
<a name="line113"></a>      pubsub.unsubscribe(&#39;foo&#39;, function() {}));
<a name="line114"></a>}
<a name="line115"></a>
<a name="line116"></a>function testSubscribeUnsubscribeWithContext() {
<a name="line117"></a>  function foo() {
<a name="line118"></a>  }
<a name="line119"></a>  function bar() {
<a name="line120"></a>  }
<a name="line121"></a>
<a name="line122"></a>  var contextA = {};
<a name="line123"></a>  var contextB = {};
<a name="line124"></a>
<a name="line125"></a>  assertEquals(&#39;Topic &quot;X&quot; must not have any subscribers&#39;, 0,
<a name="line126"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line127"></a>
<a name="line128"></a>  pubsub.subscribe(&#39;X&#39;, foo, contextA);
<a name="line129"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 1 subscriber&#39;, 1,
<a name="line130"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line131"></a>
<a name="line132"></a>  pubsub.subscribe(&#39;X&#39;, bar);
<a name="line133"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 2 subscribers&#39;, 2,
<a name="line134"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line135"></a>
<a name="line136"></a>  pubsub.subscribe(&#39;X&#39;, bar, contextB);
<a name="line137"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 3 subscribers&#39;, 3,
<a name="line138"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line139"></a>
<a name="line140"></a>  assertFalse(&#39;Unknown function/context combination return false&#39;,
<a name="line141"></a>      pubsub.unsubscribe(&#39;X&#39;, foo, contextB));
<a name="line142"></a>
<a name="line143"></a>  assertTrue(pubsub.unsubscribe(&#39;X&#39;, foo, contextA));
<a name="line144"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 2 subscribers&#39;, 2,
<a name="line145"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line146"></a>
<a name="line147"></a>  assertTrue(pubsub.unsubscribe(&#39;X&#39;, bar));
<a name="line148"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 1 subscriber&#39;, 1,
<a name="line149"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line150"></a>
<a name="line151"></a>  assertTrue(pubsub.unsubscribe(&#39;X&#39;, bar, contextB));
<a name="line152"></a>  assertEquals(&#39;Topic &quot;X&quot; must have no subscribers&#39;, 0,
<a name="line153"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line154"></a>}
<a name="line155"></a>
<a name="line156"></a>function testSubscribeOnce() {
<a name="line157"></a>  var called, context;
<a name="line158"></a>
<a name="line159"></a>  called = false;
<a name="line160"></a>  pubsub.subscribeOnce(&#39;someTopic&#39;, function() {
<a name="line161"></a>    called = true;
<a name="line162"></a>  });
<a name="line163"></a>  assertEquals(&#39;Topic must have one subscriber&#39;, 1,
<a name="line164"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line165"></a>  assertFalse(&#39;Subscriber must not have been called yet&#39;, called);
<a name="line166"></a>
<a name="line167"></a>  pubsub.publish(&#39;someTopic&#39;);
<a name="line168"></a>  assertEquals(&#39;Topic must have no subscribers&#39;, 0,
<a name="line169"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line170"></a>  assertTrue(&#39;Subscriber must have been called&#39;, called);
<a name="line171"></a>
<a name="line172"></a>  context = {called: false};
<a name="line173"></a>  pubsub.subscribeOnce(&#39;someTopic&#39;, function() {
<a name="line174"></a>    this.called = true;
<a name="line175"></a>  }, context);
<a name="line176"></a>  assertEquals(&#39;Topic must have one subscriber&#39;, 1,
<a name="line177"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line178"></a>  assertFalse(&#39;Subscriber must not have been called yet&#39;, context.called);
<a name="line179"></a>
<a name="line180"></a>  pubsub.publish(&#39;someTopic&#39;);
<a name="line181"></a>  assertEquals(&#39;Topic must have no subscribers&#39;, 0,
<a name="line182"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line183"></a>  assertTrue(&#39;Subscriber must have been called&#39;, context.called);
<a name="line184"></a>
<a name="line185"></a>  context = {called: false, value: 0};
<a name="line186"></a>  pubsub.subscribeOnce(&#39;someTopic&#39;, function(value) {
<a name="line187"></a>    this.called = true;
<a name="line188"></a>    this.value = value;
<a name="line189"></a>  }, context);
<a name="line190"></a>  assertEquals(&#39;Topic must have one subscriber&#39;, 1,
<a name="line191"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line192"></a>  assertFalse(&#39;Subscriber must not have been called yet&#39;, context.called);
<a name="line193"></a>  assertEquals(&#39;Value must have expected value&#39;, 0, context.value);
<a name="line194"></a>
<a name="line195"></a>  pubsub.publish(&#39;someTopic&#39;, 17);
<a name="line196"></a>  assertEquals(&#39;Topic must have no subscribers&#39;, 0,
<a name="line197"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line198"></a>  assertTrue(&#39;Subscriber must have been called&#39;, context.called);
<a name="line199"></a>  assertEquals(&#39;Value must have been updated&#39;, 17, context.value);
<a name="line200"></a>}
<a name="line201"></a>
<a name="line202"></a>function testSubscribeOnce_boundFn() {
<a name="line203"></a>  var context = {called: false, value: 0};
<a name="line204"></a>
<a name="line205"></a>  function subscriber(value) {
<a name="line206"></a>    this.called = true;
<a name="line207"></a>    this.value = value;
<a name="line208"></a>  }
<a name="line209"></a>
<a name="line210"></a>  pubsub.subscribeOnce(&#39;someTopic&#39;, goog.bind(subscriber, context));
<a name="line211"></a>  assertEquals(&#39;Topic must have one subscriber&#39;, 1,
<a name="line212"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line213"></a>  assertFalse(&#39;Subscriber must not have been called yet&#39;, context.called);
<a name="line214"></a>  assertEquals(&#39;Value must have expected value&#39;, 0, context.value);
<a name="line215"></a>
<a name="line216"></a>  pubsub.publish(&#39;someTopic&#39;, 17);
<a name="line217"></a>  assertEquals(&#39;Topic must have no subscribers&#39;, 0,
<a name="line218"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line219"></a>  assertTrue(&#39;Subscriber must have been called&#39;, context.called);
<a name="line220"></a>  assertEquals(&#39;Value must have been updated&#39;, 17, context.value);
<a name="line221"></a>}
<a name="line222"></a>
<a name="line223"></a>function testSubscribeOnce_partialFn() {
<a name="line224"></a>  var called = false;
<a name="line225"></a>  var value = 0;
<a name="line226"></a>
<a name="line227"></a>  function subscriber(hasBeenCalled, newValue) {
<a name="line228"></a>    called = hasBeenCalled;
<a name="line229"></a>    value = newValue;
<a name="line230"></a>  }
<a name="line231"></a>
<a name="line232"></a>  pubsub.subscribeOnce(&#39;someTopic&#39;, goog.partial(subscriber, true));
<a name="line233"></a>  assertEquals(&#39;Topic must have one subscriber&#39;, 1,
<a name="line234"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line235"></a>  assertFalse(&#39;Subscriber must not have been called yet&#39;, called);
<a name="line236"></a>  assertEquals(&#39;Value must have expected value&#39;, 0, value);
<a name="line237"></a>
<a name="line238"></a>  pubsub.publish(&#39;someTopic&#39;, 17);
<a name="line239"></a>  assertEquals(&#39;Topic must have no subscribers&#39;, 0,
<a name="line240"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line241"></a>  assertTrue(&#39;Subscriber must have been called&#39;, called);
<a name="line242"></a>  assertEquals(&#39;Value must have been updated&#39;, 17, value);
<a name="line243"></a>}
<a name="line244"></a>
<a name="line245"></a>function testSelfResubscribe() {
<a name="line246"></a>  var value = null;
<a name="line247"></a>
<a name="line248"></a>  function resubscribe(iteration, newValue) {
<a name="line249"></a>    pubsub.subscribeOnce(&#39;someTopic&#39;,
<a name="line250"></a>        goog.partial(resubscribe, iteration + 1));
<a name="line251"></a>    value = newValue + &#39;:&#39; + iteration;
<a name="line252"></a>  }
<a name="line253"></a>
<a name="line254"></a>  pubsub.subscribeOnce(&#39;someTopic&#39;, goog.partial(resubscribe, 0));
<a name="line255"></a>  assertEquals(&#39;Topic must have 1 subscriber&#39;, 1,
<a name="line256"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line257"></a>  assertNull(&#39;Value must be null&#39;, value);
<a name="line258"></a>
<a name="line259"></a>  pubsub.publish(&#39;someTopic&#39;, &#39;foo&#39;);
<a name="line260"></a>  assertEquals(&#39;Topic must have 1 subscriber&#39;, 1,
<a name="line261"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line262"></a>  assertEquals(&#39;Pubsub must not have any pending unsubscribe keys&#39;, 0,
<a name="line263"></a>      pubsub.pendingKeys_.length);
<a name="line264"></a>  assertEquals(&#39;Value be as expected&#39;, &#39;foo:0&#39;, value);
<a name="line265"></a>
<a name="line266"></a>  pubsub.publish(&#39;someTopic&#39;, &#39;bar&#39;);
<a name="line267"></a>  assertEquals(&#39;Topic must have 1 subscriber&#39;, 1,
<a name="line268"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line269"></a>  assertEquals(&#39;Pubsub must not have any pending unsubscribe keys&#39;, 0,
<a name="line270"></a>      pubsub.pendingKeys_.length);
<a name="line271"></a>  assertEquals(&#39;Value be as expected&#39;, &#39;bar:1&#39;, value);
<a name="line272"></a>
<a name="line273"></a>  pubsub.publish(&#39;someTopic&#39;, &#39;baz&#39;);
<a name="line274"></a>  assertEquals(&#39;Topic must have 1 subscriber&#39;, 1,
<a name="line275"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line276"></a>  assertEquals(&#39;Pubsub must not have any pending unsubscribe keys&#39;, 0,
<a name="line277"></a>      pubsub.pendingKeys_.length);
<a name="line278"></a>  assertEquals(&#39;Value be as expected&#39;, &#39;baz:2&#39;, value);
<a name="line279"></a>}
<a name="line280"></a>
<a name="line281"></a>function testUnsubscribeByKey() {
<a name="line282"></a>  var key1, key2, key3;
<a name="line283"></a>
<a name="line284"></a>  key1 = pubsub.subscribe(&#39;X&#39;, function() {});
<a name="line285"></a>  key2 = pubsub.subscribe(&#39;Y&#39;, function() {});
<a name="line286"></a>
<a name="line287"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 1 subscriber&#39;, 1,
<a name="line288"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line289"></a>  assertEquals(&#39;Topic &quot;Y&quot; must have 1 subscriber&#39;, 1,
<a name="line290"></a>      pubsub.getCount(&#39;Y&#39;));
<a name="line291"></a>  assertNotEquals(&#39;Subscription keys must be distinct&#39;, key1, key2);
<a name="line292"></a>
<a name="line293"></a>  pubsub.unsubscribeByKey(key1);
<a name="line294"></a>  assertEquals(&#39;Topic &quot;X&quot; must have no subscribers&#39;, 0,
<a name="line295"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line296"></a>  assertEquals(&#39;Topic &quot;Y&quot; must have 1 subscriber&#39;, 1,
<a name="line297"></a>      pubsub.getCount(&#39;Y&#39;));
<a name="line298"></a>
<a name="line299"></a>  key3 = pubsub.subscribe(&#39;X&#39;, function() {});
<a name="line300"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 1 subscriber&#39;, 1,
<a name="line301"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line302"></a>  assertEquals(&#39;Topic &quot;Y&quot; must have 1 subscriber&#39;, 1,
<a name="line303"></a>      pubsub.getCount(&#39;Y&#39;));
<a name="line304"></a>  assertNotEquals(&#39;Subscription keys must be distinct&#39;, key1, key3);
<a name="line305"></a>  assertNotEquals(&#39;Subscription keys must be distinct&#39;, key2, key3);
<a name="line306"></a>
<a name="line307"></a>  pubsub.unsubscribeByKey(key1); // Obsolete key; should be no-op.
<a name="line308"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 1 subscriber&#39;, 1,
<a name="line309"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line310"></a>  assertEquals(&#39;Topic &quot;Y&quot; must have 1 subscriber&#39;, 1,
<a name="line311"></a>      pubsub.getCount(&#39;Y&#39;));
<a name="line312"></a>
<a name="line313"></a>  pubsub.unsubscribeByKey(key2);
<a name="line314"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 1 subscriber&#39;, 1,
<a name="line315"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line316"></a>  assertEquals(&#39;Topic &quot;Y&quot; must have no subscribers&#39;, 0,
<a name="line317"></a>      pubsub.getCount(&#39;Y&#39;));
<a name="line318"></a>
<a name="line319"></a>  pubsub.unsubscribeByKey(key3);
<a name="line320"></a>  assertEquals(&#39;Topic &quot;X&quot; must have no subscribers&#39;, 0,
<a name="line321"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line322"></a>  assertEquals(&#39;Topic &quot;Y&quot; must have no subscribers&#39;, 0,
<a name="line323"></a>      pubsub.getCount(&#39;Y&#39;));
<a name="line324"></a>}
<a name="line325"></a>
<a name="line326"></a>function testSubscribeUnsubscribeMultiple() {
<a name="line327"></a>  function foo() {
<a name="line328"></a>  }
<a name="line329"></a>  function bar() {
<a name="line330"></a>  }
<a name="line331"></a>
<a name="line332"></a>  var context = {};
<a name="line333"></a>
<a name="line334"></a>  assertEquals(&#39;Pubsub channel must not have any subscribers&#39;, 0,
<a name="line335"></a>      pubsub.getCount());
<a name="line336"></a>
<a name="line337"></a>  assertEquals(&#39;Topic &quot;X&quot; must not have any subscribers&#39;, 0,
<a name="line338"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line339"></a>  assertEquals(&#39;Topic &quot;Y&quot; must not have any subscribers&#39;, 0,
<a name="line340"></a>      pubsub.getCount(&#39;Y&#39;));
<a name="line341"></a>  assertEquals(&#39;Topic &quot;Z&quot; must not have any subscribers&#39;, 0,
<a name="line342"></a>      pubsub.getCount(&#39;Z&#39;));
<a name="line343"></a>
<a name="line344"></a>  goog.array.forEach([&#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;], function(topic) {
<a name="line345"></a>    pubsub.subscribe(topic, foo);
<a name="line346"></a>  });
<a name="line347"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 1 subscriber&#39;, 1,
<a name="line348"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line349"></a>  assertEquals(&#39;Topic &quot;Y&quot; must have 1 subscriber&#39;, 1,
<a name="line350"></a>      pubsub.getCount(&#39;Y&#39;));
<a name="line351"></a>  assertEquals(&#39;Topic &quot;Z&quot; must have 1 subscriber&#39;, 1,
<a name="line352"></a>      pubsub.getCount(&#39;Z&#39;));
<a name="line353"></a>
<a name="line354"></a>  goog.array.forEach([&#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;], function(topic) {
<a name="line355"></a>    pubsub.subscribe(topic, bar, context);
<a name="line356"></a>  });
<a name="line357"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 2 subscribers&#39;, 2,
<a name="line358"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line359"></a>  assertEquals(&#39;Topic &quot;Y&quot; must have 2 subscribers&#39;, 2,
<a name="line360"></a>      pubsub.getCount(&#39;Y&#39;));
<a name="line361"></a>  assertEquals(&#39;Topic &quot;Z&quot; must have 2 subscribers&#39;, 2,
<a name="line362"></a>      pubsub.getCount(&#39;Z&#39;));
<a name="line363"></a>
<a name="line364"></a>  assertEquals(&#39;Pubsub channel must have a total of 6 subscribers&#39;, 6,
<a name="line365"></a>      pubsub.getCount());
<a name="line366"></a>
<a name="line367"></a>  goog.array.forEach([&#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;], function(topic) {
<a name="line368"></a>    pubsub.unsubscribe(topic, foo);
<a name="line369"></a>  });
<a name="line370"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 1 subscriber&#39;, 1,
<a name="line371"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line372"></a>  assertEquals(&#39;Topic &quot;Y&quot; must have 1 subscriber&#39;, 1,
<a name="line373"></a>      pubsub.getCount(&#39;Y&#39;));
<a name="line374"></a>  assertEquals(&#39;Topic &quot;Z&quot; must have 1 subscriber&#39;, 1,
<a name="line375"></a>      pubsub.getCount(&#39;Z&#39;));
<a name="line376"></a>
<a name="line377"></a>  goog.array.forEach([&#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;], function(topic) {
<a name="line378"></a>    pubsub.unsubscribe(topic, bar, context);
<a name="line379"></a>  });
<a name="line380"></a>  assertEquals(&#39;Topic &quot;X&quot; must not have any subscribers&#39;, 0,
<a name="line381"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line382"></a>  assertEquals(&#39;Topic &quot;Y&quot; must not have any subscribers&#39;, 0,
<a name="line383"></a>      pubsub.getCount(&#39;Y&#39;));
<a name="line384"></a>  assertEquals(&#39;Topic &quot;Z&quot; must not have any subscribers&#39;, 0,
<a name="line385"></a>      pubsub.getCount(&#39;Z&#39;));
<a name="line386"></a>
<a name="line387"></a>  assertEquals(&#39;Pubsub channel must not have any subscribers&#39;, 0,
<a name="line388"></a>      pubsub.getCount());
<a name="line389"></a>}
<a name="line390"></a>
<a name="line391"></a>function testPublish() {
<a name="line392"></a>  var context = {};
<a name="line393"></a>  var fooCalled = false;
<a name="line394"></a>  var barCalled = false;
<a name="line395"></a>
<a name="line396"></a>  function foo(x, y) {
<a name="line397"></a>    fooCalled = true;
<a name="line398"></a>    assertEquals(&#39;x must have expected value&#39;, &#39;x&#39;, x);
<a name="line399"></a>    assertEquals(&#39;y must have expected value&#39;, &#39;y&#39;, y);
<a name="line400"></a>  }
<a name="line401"></a>
<a name="line402"></a>  function bar(x, y) {
<a name="line403"></a>    barCalled = true;
<a name="line404"></a>    assertEquals(&#39;Context must have expected value&#39;, context, this);
<a name="line405"></a>    assertEquals(&#39;x must have expected value&#39;, &#39;x&#39;, x);
<a name="line406"></a>    assertEquals(&#39;y must have expected value&#39;, &#39;y&#39;, y);
<a name="line407"></a>  }
<a name="line408"></a>
<a name="line409"></a>  pubsub.subscribe(&#39;someTopic&#39;, foo);
<a name="line410"></a>  pubsub.subscribe(&#39;someTopic&#39;, bar, context);
<a name="line411"></a>
<a name="line412"></a>  assertTrue(pubsub.publish(&#39;someTopic&#39;, &#39;x&#39;, &#39;y&#39;));
<a name="line413"></a>  assertTrue(&#39;foo() must have been called&#39;, fooCalled);
<a name="line414"></a>  assertTrue(&#39;bar() must have been called&#39;, barCalled);
<a name="line415"></a>
<a name="line416"></a>  fooCalled = false;
<a name="line417"></a>  barCalled = false;
<a name="line418"></a>  assertTrue(pubsub.unsubscribe(&#39;someTopic&#39;, foo));
<a name="line419"></a>
<a name="line420"></a>  assertTrue(pubsub.publish(&#39;someTopic&#39;, &#39;x&#39;, &#39;y&#39;));
<a name="line421"></a>  assertFalse(&#39;foo() must not have been called&#39;, fooCalled);
<a name="line422"></a>  assertTrue(&#39;bar() must have been called&#39;, barCalled);
<a name="line423"></a>
<a name="line424"></a>  fooCalled = false;
<a name="line425"></a>  barCalled = false;
<a name="line426"></a>  pubsub.subscribe(&#39;differentTopic&#39;, foo);
<a name="line427"></a>
<a name="line428"></a>  assertTrue(pubsub.publish(&#39;someTopic&#39;, &#39;x&#39;, &#39;y&#39;));
<a name="line429"></a>  assertFalse(&#39;foo() must not have been called&#39;, fooCalled);
<a name="line430"></a>  assertTrue(&#39;bar() must have been called&#39;, barCalled);
<a name="line431"></a>}
<a name="line432"></a>
<a name="line433"></a>function testPublishEmptyTopic() {
<a name="line434"></a>  var fooCalled = false;
<a name="line435"></a>  function foo() {
<a name="line436"></a>    fooCalled = true;
<a name="line437"></a>  }
<a name="line438"></a>
<a name="line439"></a>  assertFalse(&#39;Publishing to nonexistent topic must return false&#39;,
<a name="line440"></a>      pubsub.publish(&#39;someTopic&#39;));
<a name="line441"></a>
<a name="line442"></a>  pubsub.subscribe(&#39;someTopic&#39;, foo);
<a name="line443"></a>  assertTrue(&#39;Publishing to topic with subscriber must return true&#39;,
<a name="line444"></a>      pubsub.publish(&#39;someTopic&#39;));
<a name="line445"></a>  assertTrue(&#39;Foo must have been called&#39;, fooCalled);
<a name="line446"></a>
<a name="line447"></a>  pubsub.unsubscribe(&#39;someTopic&#39;, foo);
<a name="line448"></a>  fooCalled = false;
<a name="line449"></a>  assertFalse(&#39;Publishing to topic without subscribers must return false&#39;,
<a name="line450"></a>      pubsub.publish(&#39;someTopic&#39;));
<a name="line451"></a>  assertFalse(&#39;Foo must nothave been called&#39;, fooCalled);
<a name="line452"></a>}
<a name="line453"></a>
<a name="line454"></a>function testSubscribeWhilePublishing() {
<a name="line455"></a>  // It&#39;s OK for a subscriber to add a new subscriber to its own topic,
<a name="line456"></a>  // but the newly added subscriber shouldn&#39;t be called until the next
<a name="line457"></a>  // publish cycle.
<a name="line458"></a>
<a name="line459"></a>  var firstCalled = false;
<a name="line460"></a>  var secondCalled = false;
<a name="line461"></a>
<a name="line462"></a>  pubsub.subscribe(&#39;someTopic&#39;, function() {
<a name="line463"></a>    pubsub.subscribe(&#39;someTopic&#39;, function() {
<a name="line464"></a>      secondCalled = true;
<a name="line465"></a>    });
<a name="line466"></a>    firstCalled = true;
<a name="line467"></a>  });
<a name="line468"></a>  assertEquals(&#39;Topic must have one subscriber&#39;, 1,
<a name="line469"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line470"></a>  assertFalse(&#39;No subscriber must have been called yet&#39;,
<a name="line471"></a>      firstCalled || secondCalled);
<a name="line472"></a>
<a name="line473"></a>  pubsub.publish(&#39;someTopic&#39;);
<a name="line474"></a>  assertEquals(&#39;Topic must have two subscribers&#39;, 2,
<a name="line475"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line476"></a>  assertTrue(&#39;The first subscriber must have been called&#39;,
<a name="line477"></a>      firstCalled);
<a name="line478"></a>  assertFalse(&#39;The second subscriber must not have been called yet&#39;,
<a name="line479"></a>      secondCalled);
<a name="line480"></a>
<a name="line481"></a>  pubsub.publish(&#39;someTopic&#39;);
<a name="line482"></a>  assertEquals(&#39;Topic must have three subscribers&#39;, 3,
<a name="line483"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line484"></a>  assertTrue(&#39;The first subscriber must have been called&#39;,
<a name="line485"></a>      firstCalled);
<a name="line486"></a>  assertTrue(&#39;The second subscriber must also have been called&#39;,
<a name="line487"></a>      secondCalled);
<a name="line488"></a>}
<a name="line489"></a>
<a name="line490"></a>function testUnsubscribeWhilePublishing() {
<a name="line491"></a>  // It&#39;s OK for a subscriber to unsubscribe another subscriber from its
<a name="line492"></a>  // own topic, but the subscriber in question won&#39;t actually be removed
<a name="line493"></a>  // until after publishing is complete.
<a name="line494"></a>
<a name="line495"></a>  var firstCalled = false;
<a name="line496"></a>  var secondCalled = false;
<a name="line497"></a>  var thirdCalled = false;
<a name="line498"></a>
<a name="line499"></a>  function first() {
<a name="line500"></a>    assertFalse(&#39;unsubscribe() must return false during publishing&#39;,
<a name="line501"></a>        pubsub.unsubscribe(&#39;X&#39;, second));
<a name="line502"></a>    assertEquals(&#39;Topic &quot;X&quot; must still have 3 subscribers&#39;, 3,
<a name="line503"></a>        pubsub.getCount(&#39;X&#39;));
<a name="line504"></a>    firstCalled = true;
<a name="line505"></a>  }
<a name="line506"></a>  pubsub.subscribe(&#39;X&#39;, first);
<a name="line507"></a>
<a name="line508"></a>  function second() {
<a name="line509"></a>    assertEquals(&#39;Topic &quot;X&quot; must still have 3 subscribers&#39;, 3,
<a name="line510"></a>        pubsub.getCount(&#39;X&#39;));
<a name="line511"></a>    secondCalled = true;
<a name="line512"></a>  }
<a name="line513"></a>  pubsub.subscribe(&#39;X&#39;, second);
<a name="line514"></a>
<a name="line515"></a>  function third() {
<a name="line516"></a>    assertFalse(&#39;unsubscribe() must return false during publishing&#39;,
<a name="line517"></a>        pubsub.unsubscribe(&#39;X&#39;, first));
<a name="line518"></a>    assertEquals(&#39;Topic &quot;X&quot; must still have 3 subscribers&#39;, 3,
<a name="line519"></a>        pubsub.getCount(&#39;X&#39;));
<a name="line520"></a>    thirdCalled = true;
<a name="line521"></a>  }
<a name="line522"></a>  pubsub.subscribe(&#39;X&#39;, third);
<a name="line523"></a>
<a name="line524"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 3 subscribers&#39;, 3,
<a name="line525"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line526"></a>  assertFalse(&#39;No subscribers must have been called yet&#39;,
<a name="line527"></a>      firstCalled || secondCalled || thirdCalled);
<a name="line528"></a>
<a name="line529"></a>  assertTrue(pubsub.publish(&#39;X&#39;));
<a name="line530"></a>  assertTrue(&#39;First function must have been called&#39;, firstCalled);
<a name="line531"></a>  assertTrue(&#39;Second function must have been called&#39;, secondCalled);
<a name="line532"></a>  assertTrue(&#39;Third function must have been called&#39;, thirdCalled);
<a name="line533"></a>  assertEquals(&#39;Topic &quot;X&quot; must have 1 subscriber after publishing&#39;, 1,
<a name="line534"></a>      pubsub.getCount(&#39;X&#39;));
<a name="line535"></a>  assertEquals(&#39;PubSub must not have any subscriptions pending removal&#39;, 0,
<a name="line536"></a>      pubsub.pendingKeys_.length);
<a name="line537"></a>}
<a name="line538"></a>
<a name="line539"></a>function testUnsubscribeSelfWhilePublishing() {
<a name="line540"></a>  // It&#39;s OK for a subscriber to unsubscribe itself, but it won&#39;t actually
<a name="line541"></a>  // be removed until after publishing is complete.
<a name="line542"></a>
<a name="line543"></a>  var selfDestructCalled = false;
<a name="line544"></a>
<a name="line545"></a>  function selfDestruct() {
<a name="line546"></a>    assertFalse(&#39;unsubscribe() must return false during publishing&#39;,
<a name="line547"></a>        pubsub.unsubscribe(&#39;someTopic&#39;, arguments.callee));
<a name="line548"></a>    assertEquals(&#39;Topic must still have 1 subscriber&#39;, 1,
<a name="line549"></a>        pubsub.getCount(&#39;someTopic&#39;));
<a name="line550"></a>    selfDestructCalled = true;
<a name="line551"></a>  }
<a name="line552"></a>
<a name="line553"></a>  pubsub.subscribe(&#39;someTopic&#39;, selfDestruct);
<a name="line554"></a>  assertEquals(&#39;Topic must have 1 subscriber&#39;, 1,
<a name="line555"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line556"></a>  assertFalse(&#39;selfDestruct() must not have been called yet&#39;,
<a name="line557"></a>      selfDestructCalled);
<a name="line558"></a>
<a name="line559"></a>  pubsub.publish(&#39;someTopic&#39;);
<a name="line560"></a>  assertTrue(&#39;selfDestruct() must have been called&#39;, selfDestructCalled);
<a name="line561"></a>  assertEquals(&#39;Topic must have no subscribers after publishing&#39;, 0,
<a name="line562"></a>      pubsub.getCount(&#39;someTopic&#39;));
<a name="line563"></a>  assertEquals(&#39;PubSub must not have any subscriptions pending removal&#39;, 0,
<a name="line564"></a>      pubsub.pendingKeys_.length);
<a name="line565"></a>}
<a name="line566"></a>
<a name="line567"></a>function testPublishReturnValue() {
<a name="line568"></a>  pubsub.subscribe(&#39;X&#39;, function() {
<a name="line569"></a>    pubsub.unsubscribe(&#39;X&#39;, arguments.callee);
<a name="line570"></a>  });
<a name="line571"></a>  assertTrue(&#39;publish() must return true even if the only subscriber &#39; +
<a name="line572"></a>      &#39;removes itself during publishing&#39;, pubsub.publish(&#39;X&#39;));
<a name="line573"></a>}
<a name="line574"></a>
<a name="line575"></a>function testNestedPublish() {
<a name="line576"></a>  var x1 = false;
<a name="line577"></a>  var x2 = false;
<a name="line578"></a>  var y1 = false;
<a name="line579"></a>  var y2 = false;
<a name="line580"></a>
<a name="line581"></a>  pubsub.subscribe(&#39;X&#39;, function() {
<a name="line582"></a>    pubsub.publish(&#39;Y&#39;);
<a name="line583"></a>    pubsub.unsubscribe(&#39;X&#39;, arguments.callee);
<a name="line584"></a>    x1 = true;
<a name="line585"></a>  });
<a name="line586"></a>
<a name="line587"></a>  pubsub.subscribe(&#39;X&#39;, function() {
<a name="line588"></a>    x2 = true;
<a name="line589"></a>  });
<a name="line590"></a>
<a name="line591"></a>  pubsub.subscribe(&#39;Y&#39;, function() {
<a name="line592"></a>    pubsub.unsubscribe(&#39;Y&#39;, arguments.callee);
<a name="line593"></a>    y1 = true;
<a name="line594"></a>  });
<a name="line595"></a>
<a name="line596"></a>  pubsub.subscribe(&#39;Y&#39;, function() {
<a name="line597"></a>    y2 = true;
<a name="line598"></a>  });
<a name="line599"></a>
<a name="line600"></a>  pubsub.publish(&#39;X&#39;);
<a name="line601"></a>
<a name="line602"></a>  assertTrue(&#39;x1 must be true&#39;, x1);
<a name="line603"></a>  assertTrue(&#39;x2 must be true&#39;, x2);
<a name="line604"></a>  assertTrue(&#39;y1 must be true&#39;, y1);
<a name="line605"></a>  assertTrue(&#39;y2 must be true&#39;, y2);
<a name="line606"></a>}
<a name="line607"></a>
<a name="line608"></a>function testClear() {
<a name="line609"></a>  function fn() {
<a name="line610"></a>  }
<a name="line611"></a>
<a name="line612"></a>  goog.array.forEach([&#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;], function(topic) {
<a name="line613"></a>    pubsub.subscribe(topic, fn);
<a name="line614"></a>  });
<a name="line615"></a>  assertEquals(&#39;Pubsub channel must have 4 subscribers&#39;, 4,
<a name="line616"></a>      pubsub.getCount());
<a name="line617"></a>
<a name="line618"></a>  pubsub.clear(&#39;W&#39;);
<a name="line619"></a>  assertEquals(&#39;Pubsub channel must have 3 subscribers&#39;, 3,
<a name="line620"></a>      pubsub.getCount());
<a name="line621"></a>
<a name="line622"></a>  goog.array.forEach([&#39;X&#39;, &#39;Y&#39;], function(topic) {
<a name="line623"></a>    pubsub.clear(topic);
<a name="line624"></a>  });
<a name="line625"></a>  assertEquals(&#39;Pubsub channel must have 1 subscriber&#39;, 1,
<a name="line626"></a>      pubsub.getCount());
<a name="line627"></a>
<a name="line628"></a>  pubsub.clear();
<a name="line629"></a>  assertEquals(&#39;Pubsub channel must have no subscribers&#39;, 0,
<a name="line630"></a>      pubsub.getCount());
<a name="line631"></a>}
</pre>


</body>
</html>
